package com.example.consumptionrecords.util.fastjson;

import com.baidubce.qianfan.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;

public class FastJsonUtils {

    private static ObjectMapper om = new ObjectMapper();


    public static String objToString(Object obj) {
        String result = "";
        try {
            result = om.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static <T> T parseObj(String json, Class<T> clazz) {
        T t = null;
        if (StringUtils.isNotEmpty(json)) {
            try {
                om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
                t = om.readValue(json.getBytes(), clazz);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return t;
    }
}
